/*
 * Copyright 2023-2025 Licensed under the Dual Licensing
 * website: https://aizuda.com
 */
package com.aizuda.bpm.beetlsql.impl;

import com.aizuda.bpm.beetlsql.mapper.FlwTaskActorMapper;
import com.aizuda.bpm.engine.dao.FlwTaskActorDao;
import com.aizuda.bpm.engine.entity.FlwTaskActor;
import com.github.yitter.idgen.YitIdHelper;

import java.util.List;

/**
 * 任务参与者数据访问层接口实现类
 *
 * <p>
 * <a href="https://aizuda.com">官网</a>尊重知识产权，不允许非法使用，后果自负，不允许非法使用，后果自负
 * </p>
 *
 * @author hubin
 * @since 1.0
 */
public class FlwTaskActorDaoImpl implements FlwTaskActorDao {
    private final FlwTaskActorMapper taskActorMapper;

    public FlwTaskActorDaoImpl(FlwTaskActorMapper taskActorMapper) {
        this.taskActorMapper = taskActorMapper;
    }

    @Override
    public boolean insert(FlwTaskActor taskActor) {
        taskActor.setId(YitIdHelper.nextId());
        return taskActorMapper.createQuery().insertSelective(taskActor) > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return taskActorMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteByIds(List<Long> ids) {
        return taskActorMapper.createLambdaQuery().andIn(FlwTaskActor::getId, ids).delete()>0;
    }

    @Override
    public boolean deleteByTaskId(Long taskId) {
        return taskActorMapper.createLambdaQuery().andEq(FlwTaskActor::getTaskId, taskId).delete()>0;
    }

    @Override
    public boolean deleteByInstanceIdAndTaskIds(Long instanceId, List<Long> taskIds) {
        return taskActorMapper.createLambdaQuery()
                .andEq(FlwTaskActor::getInstanceId, instanceId)
                .andIn(FlwTaskActor::getTaskId, taskIds).delete() > 0;
    }

    @Override
    public boolean deleteByInstanceIds(List<Long> instanceIds) {
        return taskActorMapper.createLambdaQuery().andIn(FlwTaskActor::getInstanceId, instanceIds).delete()>0;
    }

    @Override
    public boolean deleteByTaskIdAndAgentType(Long taskId, int agentType) {
        return taskActorMapper.createLambdaQuery().andEq(FlwTaskActor::getTaskId, taskId)
                .andEq(FlwTaskActor::getAgentType, agentType).delete()>0;
    }

    @Override
    public boolean deleteByTaskIdAndActorIds(Long taskId, List<String> actorIds) {
        return taskActorMapper.createLambdaQuery().andEq(FlwTaskActor::getTaskId, taskId)
                .andIn(FlwTaskActor::getActorId, actorIds).delete()>0;
    }

    @Override
    public boolean updateById(FlwTaskActor taskActor) {
        return taskActorMapper.createLambdaQuery().andEq(FlwTaskActor::getId, taskActor.getId()).updateSelective(taskActor)>0;
    }

    @Override
    public List<FlwTaskActor> selectListByInstanceId(Long instanceId) {
        return taskActorMapper.createLambdaQuery().andEq(FlwTaskActor::getInstanceId, instanceId).select();
    }

    @Override
    public List<FlwTaskActor> selectListByTaskId(Long taskId) {
        return taskActorMapper.createLambdaQuery().andEq(FlwTaskActor::getTaskId, taskId).select();
    }

    @Override
    public List<FlwTaskActor> selectListByTaskIds(List<Long> taskIds) {
        return taskActorMapper.createLambdaQuery().andIn(FlwTaskActor::getTaskId, taskIds).select();
    }

    @Override
    public List<FlwTaskActor> selectListByTaskIdAndActorId(Long taskId, String actorId) {
        return taskActorMapper.createLambdaQuery().andEq(FlwTaskActor::getTaskId, taskId)
                .andEq(FlwTaskActor::getActorId, actorId).select();
    }

    @Override
    public List<FlwTaskActor> selectListByActorId(String actorId) {
        return taskActorMapper.createLambdaQuery().andEq(FlwTaskActor::getActorId, actorId).select();
    }

    @Override
    public Long selectCountByTaskIdAndActorId(Long taskId, String actorId) {
        return taskActorMapper.createLambdaQuery().andEq(FlwTaskActor::getTaskId, taskId)
                .andEq(FlwTaskActor::getActorId, actorId).count();
    }
}
